from utils.mydb import db1

class ACL:
    def __init__(self):
        pass

    def add_resource(self,uid,reslist):
        for i in reslist:
            sql = "insert into user_resource(user_id,resource_id) values(%d,%d)"%(int(uid),int(i))
            db1.update(sql)

    def change_resource(self,uid,reslist):
        sql = "delete from user_resource where user_id=%d"%(int(uid))
        db1.update(sql)
        for i in reslist:
            sql1 = "update user_resource set resource_id=%d where user_id=%d"%(int(i),int(uid))
            db1.update(sql1)

    def del_resource(self,uid,resid):
        sql = "delete from user_resource where user_id=%d and resource_id=%d"%(int(uid),int(resid))
        db1.update(sql)


    def get_page_reslist(self,uid):
        sql = "select url from user_resource left join resources on user_resource.resource_id=resources.id where user_id=%d and resources.type=1"%int(uid)
        res = db1.find_all(sql)
        lst=[]
        for i in res:
            lst.append(i["url"])
        return lst

    def get_reslist(self,uid):
        sql = "select url from user_resource left join resources on user_resource.resource_id=resources.id where user_id=%d and resources.type=2" % int(uid)
        res = db1.find_all(sql)
        lst = []
        for i in res:
            lst.append(i["url"])
        return lst

acl = ACL()

# print(acl.get_page_reslist(1))
print(acl.get_reslist(1))
